package com.wuliang.generate.common.core.utils;

import cn.hutool.captcha.AbstractCaptcha;
import cn.hutool.captcha.CircleCaptcha;
import cn.hutool.captcha.LineCaptcha;
import cn.hutool.captcha.ShearCaptcha;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.math.RandomUtils;
import org.springframework.context.annotation.Lazy;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 邬魉
 */
@Lazy
@Slf4j
public class VerificationCodeUtils {

    private static final Integer CODE_WIDTH = 114;

    private static final Integer CODE_HEIGHT = 64;

    private static final List<AbstractCaptcha> CAPTCHA_LIST = initCaptcha();

    private static List<AbstractCaptcha> initCaptcha() {
        List<AbstractCaptcha> captchaList = new ArrayList<>();
        captchaList.add(new ShearCaptcha(CODE_WIDTH, CODE_HEIGHT));
        captchaList.add(new LineCaptcha(CODE_WIDTH, CODE_HEIGHT));
        captchaList.add(new CircleCaptcha(CODE_WIDTH, CODE_HEIGHT));
        return captchaList;
    }

    public static AbstractCaptcha createCaptcha() {
        AbstractCaptcha captcha = CAPTCHA_LIST.get(RandomUtils.nextInt() % CAPTCHA_LIST.size());
        captcha.createCode();
        return captcha;
    }

    public static void main(String[] args) {
    }

}
